package com.tsingtao.springboot2.service.impl;

import com.tsingtao.springboot2.dto.Company;
import com.tsingtao.springboot2.mapper.CompanyMapper;
import com.tsingtao.springboot2.service.CompanyService;
import com.tsingtao.springboot2.utils.MyMapperProxy;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Proxy;
import java.util.List;

@Slf4j
@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private SqlSession sqlSession;

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public List<Company> selectAll() {
        return companyMapper.selectAll();
    }

    @Override
    public List<Company> selectAllByProxy() {
        MyMapperProxy<CompanyMapper> companyMyMapperProxy = new MyMapperProxy<>(CompanyMapper.class, sqlSession);
        CompanyMapper proxyCompanyMapper = (CompanyMapper) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{CompanyMapper.class}, companyMyMapperProxy);
        return proxyCompanyMapper.selectAll();
    }

    @Override
    public List<Company> selectByCode(String code) {
        return companyMapper.selectByCode(code);
    }

    @Override
    public List<Company> selectByChoose(Company company) {
        return companyMapper.selectByChoose(company);
    }

    @Override
    public List<Company> selectByCodeOrName(String code, String name) {
        return companyMapper.selectByCodeOrName(code, name);
    }

    @Override
    public Company insert(Company company) {
        int count = companyMapper.insert(company);
        log.info("插入 {} 条数据", count);
        return company;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public List<Company> batchInsertByForeach(List<Company> list) {
        int count = companyMapper.batchInsertByForeach(list);
        log.info("插入 {} 条数据", count);
        return list;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public List<Company> batchInsert(List<Company> companies) {
        if(CollectionUtils.isEmpty(companies)) {
            throw new RuntimeException("请传递参数！！！");
        }
        int count = 0;
        for (Company company : companies) {
//            count = count + companyMapper.insert(company);
            count += companyMapper.insert(company);
        }
        log.info("插入 {} 条数据", count);
        return companies;
    }




    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Company update(Company company) {
        int count = companyMapper.update(company);
        log.info("更新 {} 条数据", count);
        return company;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int deleteById(Long id) {
        int count = companyMapper.deleteById(id);
        log.info("删除 {} 条数据", count);
        return count;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int batchDeleteByIds(List<Company> list) {
        return companyMapper.batchDeleteByIds(list);
    }
}
